package com.lelaohui.net.netmanager;


import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.lelaohui.net.utils.LoggerInterceptor;
import com.lelaohui.net.utils.NetInterceptor;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by T460p on 2017/3/11.
 */

public class NetManager {

    private Config config =null;
    private Retrofit.Builder retrofitBuilder=null;
    private NetManager(){
        builder=new OkHttpClient.Builder();
        setOkBuilder(new Config());
    }
    public Config getConfig() {
        return config;
    }

    private OkHttpClient.Builder builder=null;


   public static  class NetManagerBuilder{
       private NetManager netManager=null;
       public NetManagerBuilder(){
           netManager=new NetManager();
       }
        public NetManagerBuilder buildConfig(Config config){
            if (config == null) {
                config=new Config();
            }
            netManager. setOkBuilder(config);
            return this;
        }
        public NetManagerBuilder buildRetrofitBuilderUrl(String url){
            netManager.initRetrofitBuilder();
            netManager.retrofitBuilder.baseUrl(url);
            return this;
        }
        public NetManagerBuilder buildConverterFactory(Converter.Factory factory){
            netManager.initRetrofitBuilder();
            netManager. retrofitBuilder.addConverterFactory(factory);
            return this;
        }
        public NetManager build(){
            return netManager;
        }

    }
    private void initRetrofitBuilder() {
        if(retrofitBuilder==null){
            retrofitBuilder=new Retrofit.Builder();
            retrofitBuilder.client(NetManager.this.builder.build());
            retrofitBuilder .addConverterFactory(GsonConverterFactory.create());
            retrofitBuilder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
        }
    }
    private void setOkBuilder(Config config) {
        if (config != null) {
            if(config.cacheFile!=null){

                builder.cache(new Cache(config.cacheFile,config.cacheMaxSize));
            }
        }
        builder.readTimeout(config.readTimeout, TimeUnit.SECONDS);
        builder.writeTimeout(config.writeTime,TimeUnit.SECONDS);
        builder.connectTimeout(config.connectTimeOutOut,TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(config.retryOnConnectionFailure);
//        builder.addInterceptor(new HttpLoggingInterceptor());//日志
        builder.addInterceptor(new LoggerInterceptor("TAG"));//日志
        builder.addNetworkInterceptor(new NetInterceptor());
        if (config.interceptor != null) {
            builder.addInterceptor(config.interceptor);
        }
    }

    public Retrofit getRetrofit(){
        initRetrofitBuilder();
       return retrofitBuilder.build();
    }
    public static class Config{
        /**
         * 缓存文件
         */
       private File cacheFile;
        /**
         * 缓存大小
         */
        private int cacheMaxSize=10*1024*1024;
        /**
         * 访问失败之后是否重试
         */
        private boolean retryOnConnectionFailure=false;
        /**
         * 连接超时时间
         */
        private long connectTimeOutOut=20;
        /**
         * 写超时时间
         */
        private long writeTime=60;
        /**
         * 读超时时间
         */
        private long readTimeout=60;

        public File getCacheFile() {
            return cacheFile;
        }

        public void setCacheFile(File cacheFile) {
            this.cacheFile = cacheFile;
        }

        public int getCacheMaxSize() {
            return cacheMaxSize;
        }

        public void setCacheMaxSize(int cacheMaxSize) {
            this.cacheMaxSize = cacheMaxSize;
        }

        public boolean isRetryOnConnectionFailure() {
            return retryOnConnectionFailure;
        }

        public void setRetryOnConnectionFailure(boolean retryOnConnectionFailure) {
            this.retryOnConnectionFailure = retryOnConnectionFailure;
        }

        public long getConnectTimeOutOut() {
            return connectTimeOutOut;
        }

        public void setConnectTimeOutOut(long connectTimeOutOut) {
            this.connectTimeOutOut = connectTimeOutOut;
        }

        public long getWriteTime() {
            return writeTime;
        }

        public void setWriteTime(long writeTime) {
            this.writeTime = writeTime;
        }

        public long getReadTimeout() {
            return readTimeout;
        }

        public void setReadTimeout(long readTimeout) {
            this.readTimeout = readTimeout;
        }

        public Interceptor getInterceptor() {
            return interceptor;
        }

        public void setInterceptor(Interceptor interceptor) {
            this.interceptor = interceptor;
        }

        /***
         * 拦截器
         *
         */
        private  Interceptor interceptor=null;
    }

}
